var $callTable = $('#callTable');//呼叫表单
var $converTable = $('#converTable');//

$(function(){
    getData(4,'','');
    $('.datepicker').datepicker({
        language: "zh-CN",
        autoclose: true,
        clearBtn: true,
        todayHighlight: true,
        format: "yyyy-mm-dd"
    });

    isSuperAdmin();
})

//防止表头与表格不对齐
$(window).resize(function () {
    $callTable.bootstrapTable('resetView');
    $converTable.bootstrapTable('resetView');

});

$('#customDateBtn').on('click',function () {
    getData('',$('#startTm').val(),$('#endTm').val())
})


//管理系统隐藏compNamediv
function isSuperAdmin() {
    $.ajax({
        type: 'GET',
        dataType: "json",
        url: '/returnMoney/isSuperAdmin',
        cache: false,
        async: false,
        success: (function (data) {
            if (!data.data) {//普管
                document.getElementById("converTableDiv").style.display = "none";
                callTableInitData()//呼叫表格
            }else{//超管
                $(".call-all").hide()
                // document.getElementById("callTableDiv").style.display = "none";
                converTableInitData()
            }
        }),
        error: (function (res) {
            alert("请求失败");
        })
    });
}


function selTime(time) {
    if (time == 'topMonth') {//上月
        $('.topMonth').addClass('active-second');
        $('.month').removeClass('active-second');
        $('.week').removeClass('active-second');
        $('.customize').removeClass('active-second');
        $("#clientDataType").val("topMonth");
        $('#customInput').css('display','none')
        getData(4,'','');
        $('#startTm').val('');
        $('#endTm').val('')
    }
    if (time == 'month') {//本月
        $('.month').addClass('active-second');
        $('.topMonth').removeClass('active-second');
        $('.week').removeClass('active-second');
        $('.customize').removeClass('active-second');
        $("#clientDataType").val("year");
        $('#customInput').css('display','none')
        getData(3,'','');
        $('#contractDate').val('');
        $('#startTm').val('');
        $('#endTm').val('')
    }
    if (time == 'week') {//本周
        $('.week').addClass('active-second');
        $('.topMonth').removeClass('active-second');
        $('.month').removeClass('active-second');
        $('.customize').removeClass('active-second');
        $("#clientDataType").val("year");
        $('#customInput').css('display','none')
        getData(2,'','');
        $('#startTm').val('');
        $('#endTm').val('')
    }
    if (time == 'customize') {//本周
        $('.customize').addClass('active-second');
        $('.topMonth').removeClass('active-second');
        $('.month').removeClass('active-second');
        $('.week').removeClass('active-second');
        $('#customInput').css('display','block')
    }
}

function  getData(type,start,end) {
    $.ajax({
        type: 'post',
        url: '/verifyPhone/getVerifyPhoneReport',
        dataType: "json",
        // contentType: "application/json;charset=utf-8",
        data: {
            dateUnit : type,
            startTm:start,
            endTm:end
        },
        success: function (result) {
            if(result.code == '000000') {
                  let list = [
                      ['product', '小于30秒', '大于30秒小于60秒', '大于60秒小于90秒', '大于90秒']
                  ]
                $.each(result.data,function(index,item){
                    if(item.le30SecondCount == 0 && item.ge60lt90SecondCount == 0
                        && item.ge90SecondCount == 0 && item.ge30lt60SecondCount == 0){
                        return;
                    }else{
                        var array = [];
                        array.push(item.userName)
                        array.push(item.le30SecondCount)
                        array.push(item.ge30lt60SecondCount)
                        array.push(item.ge60lt90SecondCount)
                        array.push(item.ge90SecondCount)
                        list.push(array)
                    }
                })
                // $.each(result.data,function(index,item){
                //
                // })
                if(list.length>10){
                    var dataZoom = [
                        {
                            type: 'slider',
                            show: true,
                            start:94,
                            end: 100,
                            handleSize: '100%',
                            realtime: true,
                        },
                        {
                            type: 'inside',
                            start: 0,
                            end: 100
                        },
                    ]
                    chartsInit('verifyPhone',list,dataZoom)
                }else{
                    chartsInit('verifyPhone',list,[])
                }

            }
        },
        error: function(){
            alert('请求失败')
        }
    });
}


function chartsInit(id,list,dataZoom){
    let dom = document.getElementById(id);
    let myChart = echarts.init(dom);
    let option = {
        // legend: {},
        tooltip: {},
        color:['#9580FF','#6D93FF','#52CCA3','#32CDFF'],
        dataset: {
            source:list
            // source: [
            //     ['product', '小于30', '大于60小于90', '大于90'],
            //     ['Matcha Latte', 43.3, 85.8, 93.7],
            //     ['Milk Tea', 83.1, 73.4, 55.1],
            //     ['Cheese Cocoa', 86.4, 65.2, 82.5],
            //     ['Walnut Brownie', 72.4, 53.9, 39.1]
            // ]
        },
        xAxis: {
            type: 'category',
            name:'姓名',
            nameTextStyle:{
                color:"#999999",
                fontSize:12,
            },
            axisLine: {
                show: true,
                lineStyle:{
                    color:'rgba(83, 83, 83,0.1)',
                },
            },
            axisTick:{
                show: false,
            },
            splitArea: {
                // show: true,
                color: '#f00',
                lineStyle: {
                    color: '#f00'
                },
            },
            axisLabel: {
                color: '#999999',
            },
            splitLine: {
                show: false
            },
        },
        yAxis: {
            name:'个数',
            minInterval:1,
            nameTextStyle:{
                color:"#999999",
                fontSize:12,
            },

            axisLine: {
                //坐标轴轴线相关设置
                show: true,
                lineStyle:{
                    color:'rgba(83, 83, 83,0.1)',
                },
            },
            plitLine: {
                //分隔线
                show: false,
                lineStyle: {
                    //坐标轴在 grid 区域中的分隔线。
                    color: 'rgba(83, 83, 83,0.1)'
                }
            },
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#999999',
                },
            },
            splitLine: {
                show: false
            },
            axisTick: {
                show: false,
            },
        },
        series: [
            {type: 'bar',barMaxWidth:'40'},
            {type: 'bar',barMaxWidth:'40'},
            {type: 'bar',barMaxWidth:'40'},
            {type: 'bar',barMaxWidth:'40'}
        ],
        gird:{
            bottom:'20%'
        },
        dataZoom :dataZoom,

    };
    myChart.setOption(option);
    window.addEventListener("resize",function(){
        myChart.resize();
    });

}


/**
 *呼叫中心
 */
function callTableInitData() {
    //先销毁表格
    $callTable.bootstrapTable('destroy');
    $callTable.bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: '/verifyPhone/getDurationByCompanyId',//请求地址
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",//请求内容类型
        striped: true,//是否显示行间隔色
        cache: false,//是否使用缓存
        showColumns: false,//显示隐藏列
        showRefresh: false,//显示刷新按钮
        showToggle: false,//切换显示样式
        showFooter: false,//显示Table脚部
        pagination: true,//是否显示分页
        showPaginationSwitch: false,//是否显示分页按钮
        clickToSelect: false,//是否启用点击选中行
        singleSelect: false,
        sidePagination: "server",//分页方式：client客户端分页，server服务端分页
        pageSize: 10,//每页的记录行数
        pageNumber: 1,//初始化加载第1页，默认第1页
        minimumCountColumns: 2,//最少要显示的列数
        undefinedText: '-',//cell没有值时显示
        queryParams: callQueryParams,
        columns: [
            {title: '姓名', field: 'userName', align: 'left', valign: 'middle'},
            {title: '小于30秒', field: 'le30SecondCount', align: 'center', valign: 'middle'},
            {title: '30秒<通话时长<60秒', field: 'ge30lt60SecondCount', align: 'center', valign: 'middle'},
            {title: '60秒<通话时长<90秒', field: 'ge60lt90SecondCount', align: 'center', valign: 'middle'},
            {title: '大于90秒', field: 'ge90SecondCount', align: 'center', valign: 'middle'},
            {title: '总通话秒数', field: 'sumDuration', align: 'center', valign: 'middle'},
        ],
        responseHandler: function (result) {
            if (result.code == "000000") {
                var obj = {
                    "rows": result.data,
                    "total": result.totalRecords//总条数
                };
            } else {
                var obj = {
                    "total": 0,
                    "rows": [],
                }
            }
            return obj;
        },
        onLoadSuccess: function () {

        },
        onLoadError: function (status) {
            if (status == 403) {
                alert("权限不足！禁止访问！");
            } else {
                //加载失败时执行
                alert("加载失败！");
            }
        },
        formatLoadingMessage: function () {
            return "请稍等，加载中...";
        },
        formatNoMatches: function () {
            return '无符合条件的记录';
        },
    })
}

/**
 * --列表序列号
 */
function indexFormat(value, row, index) {
    return index + 1;
}

function converTableInitData() {
    //先销毁表格
    $converTable.bootstrapTable('destroy');
    $converTable.bootstrapTable({
        classes: 'table table-hover table-bordered table-striped',
        url: '/verifyPhone/getAllDurationGroupByCompanyId',//请求地址
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",//请求内容类型
        dataType: "json",//数据类型
        striped: true,//是否显示行间隔色
        // sortable: true,//是否启用排序
        // sortOrder: "desc",//排序方式
        // sortName: 'createTm',//排序字段
        cache: false,//是否使用缓存
        showColumns: false,//显示隐藏列
        showRefresh: false,//显示刷新按钮
        showToggle: false,//切换显示样式
        showFooter: false,//显示Table脚部
        pagination: false,//是否显示分页
        showPaginationSwitch: false,//是否显示分页按钮
        clickToSelect: false,//是否启用点击选中行
        singleSelect: false,
        minimumCountColumns: 2,//最少要显示的列数
        undefinedText: '-',//cell没有值时显示
        // sidePagination: "server",//分页方式：client客户端分页，server服务端分页
        // uniqueId: 'productOrderId',
        // idField: 'productOrderId',
        // pageSize: 5,//每页的记录行数
        // pageNumber: 1,//初始化加载第1页，默认第1页
        queryParams: converQueryParams,//查询参数
        columns: [
            {title: '所属分公司',field: 'companyName', align: 'left', valign: 'middle' },
            {title: '通话秒数',field: 'sum', align: 'left', valign: 'middle' },
            {title: '通话员工数',field: 'pnum', align: 'left', valign: 'middle'},
            {title: '员工平均通话数',field: 'anum', align: 'left', valign: 'middle' },
            {title: '通话秒数>0',field: '30s', align: 'left', valign: 'middle' },
            {title: '通话秒数>30',field: '60s', align: 'left', valign: 'middle' },
            {title: '通话秒数>60',field: '90s', align: 'left', valign: 'middle' },
            {title: '通话秒数>90',field: '90sMore', align: 'left', valign: 'middle' },

        ],
        responseHandler: function (result) {
            if (result.code == "000000") {
                var obj = {
                    "rows": result.data,
                    "total": result.totalRecords//总条数
                };
            } else {
                var obj = {
                    "total": 0,
                    "rows": [],
                }
            }
            return obj;
        },
        onLoadSuccess: function () {

        },

        onPostBody: function (data) {
        },
        onLoadError: function (status) {
            if (status == 403) {
                alert("权限不足！禁止访问！");
            } else {
                //加载失败时执行
                alert("加载失败！");
            }
        },
        formatLoadingMessage: function () {
            return "请稍等，加载中...";
        },
        formatNoMatches: function () {
            return '无符合条件的记录';
        },
    });
}

function callTableSelTime(type) {
    if (type == 'topMonth') {
        $('.callTableTopMonth').addClass('active-second');
        $('.callTableMonth').removeClass('active-second');
        $('.callTableWeek').removeClass('active-second');
        $('.callTableTopWeek').removeClass('active-second');
        $("#callDateUnit").val(1);
        $callTable.bootstrapTable('refresh');
    }
    if (type == 'month') {
        $('.callTableTopMonth').removeClass('active-second');
        $('.callTableMonth').addClass('active-second');
        $('.callTableWeek').removeClass('active-second');
        $('.callTableTopWeek').removeClass('active-second');
        $("#callDateUnit").val(2);
        $callTable.bootstrapTable('refresh');
    }
    if (type == 'week') {
        $('.callTableTopMonth').removeClass('active-second');
        $('.callTableMonth').removeClass('active-second');
        $('.callTableWeek').addClass('active-second');
        $('.callTableTopWeek').removeClass('active-second');
        $("#callDateUnit").val(3);
        $callTable.bootstrapTable('refresh');
    }
    if (type == 'topWeek') {
        $('.callTableTopMonth').removeClass('active-second');
        $('.callTableMonth').removeClass('active-second');
        $('.callTableWeek').removeClass('active-second');
        $('.callTableTopWeek').addClass('active-second');
        $("#callDateUnit").val(4);
        $callTable.bootstrapTable('refresh');
    }
}


function converTableSelTime(type) {
    if (type == 'topMonth') {
        $('.converTableTopMonth').addClass('active-second');
        $('.converTableMonth').removeClass('active-second');
        $('.converTableWeek').removeClass('active-second');
        $('.converTableTopWeek').removeClass('active-second');
        $("#converDateUnit").val(1);
        $converTable.bootstrapTable('refresh');
    }
    if (type == 'month') {
        $('.converTableTopMonth').removeClass('active-second');
        $('.converTableMonth').addClass('active-second');
        $('.converTableWeek').removeClass('active-second');
        $('.converTableTopWeek').removeClass('active-second');
        $("#converDateUnit").val(2);
        $converTable.bootstrapTable('refresh');
    }
    if (type == 'week') {
        $('.converTableTopMonth').removeClass('active-second');
        $('.converTableMonth').removeClass('active-second');
        $('.converTableWeek').addClass('active-second');
        $('.converTableTopWeek').removeClass('active-second');
        $("#converDateUnit").val(3);
        $converTable.bootstrapTable('refresh');
    }
    if (type == 'topWeek') {
        $('.converTableTopMonth').removeClass('active-second');
        $('.converTableMonth').removeClass('active-second');
        $('.converTableWeek').removeClass('active-second');
        $('.converTableTopWeek').addClass('active-second');
        $("#converDateUnit").val(4);
        $converTable.bootstrapTable('refresh');
    }

}

/**
 * 查询参数
 */
function callQueryParams(params) {
    params.pageSize = params.limit;
    params.pageIndex = (params.offset / params.limit) + 1;
    //搜索条件
    params.dateUnit = Number($("#callDateUnit").val());
    return params;
}
/**
 * 查询参数
 */
function converQueryParams() {
    var param = {};
    //搜索条件
    param.dateUnit = Number($("#converDateUnit").val());
    return param;
}


